# coding:utf-8
from typing import Union
from PySide6.QtCore import Qt, Signal
from PySide6.QtGui import QIcon, QColor
from PySide6.QtWidgets import QWidget, QLabel, QButtonGroup, QVBoxLayout, QPushButton, QHBoxLayout, QFileDialog
from qfluentwidgets import ExpandGroupSettingCard, ConfigItem, FluentIconBase, RadioButton

from common import cfg


class WallpaperCard(ExpandGroupSettingCard):
    """ Custom color setting card """

    fileChanged = Signal(str)

    def __init__(
            self,
            configItem: ConfigItem,
            icon: Union[str, QIcon, FluentIconBase],
            title: str,
            content=None,
            parent=None
    ):
        """
        Parameters
        ----------
        configItem: ConfigItem
            options config item

        icon: str | QIcon | FluentIconBase
            the icon to be drawn

        title: str
            the title of setting card

        content: str
            the content of setting card

        parent: QWidget
            parent window
        """
        super().__init__(icon, title, content, parent=parent)
        self.configItem = configItem
        self.default = configItem.defaultValue
        self.custom = cfg.get(configItem)

        self.choiceLabel = QLabel(self)

        self.radioWidget = QWidget(self.view)
        self.radioLayout = QVBoxLayout(self.radioWidget)
        self.defaultRadioButton = RadioButton('默认背景', self.radioWidget)
        self.customRadioButton = RadioButton('自定义背景', self.radioWidget)
        self.buttonGroup = QButtonGroup(self)

        self.customWidget = QWidget(self.view)
        self.customLayout = QHBoxLayout(self.customWidget)
        self.customLabel = QLabel('自定义颜色', self.customWidget)
        self.chooseButton = QPushButton('选择背景', self.customWidget)

        self.__initWidget()

    def __initWidget(self):
        self.__initLayout()

        if self.default != self.custom:
            self.customRadioButton.setChecked(True)
            self.chooseButton.setEnabled(True)
        else:
            self.defaultRadioButton.setChecked(True)
            self.chooseButton.setEnabled(False)

        self.choiceLabel.setText(self.buttonGroup.checkedButton().text())
        self.choiceLabel.adjustSize()

        self.choiceLabel.setObjectName("titleLabel")
        self.customLabel.setObjectName("titleLabel")
        self.chooseButton.setObjectName('chooseColorButton')

        self.buttonGroup.buttonClicked.connect(self.__onRadioButtonClicked)
        self.chooseButton.clicked.connect(self.__showFileDialog)

    def __initLayout(self):
        self.addWidget(self.choiceLabel)

        self.radioLayout.setSpacing(19)
        self.radioLayout.setAlignment(Qt.AlignmentFlag.AlignTop)
        self.radioLayout.setContentsMargins(48, 18, 0, 18)
        self.buttonGroup.addButton(self.customRadioButton)
        self.buttonGroup.addButton(self.defaultRadioButton)
        self.radioLayout.addWidget(self.customRadioButton)
        self.radioLayout.addWidget(self.defaultRadioButton)
        self.radioLayout.setSizeConstraint(QVBoxLayout.SizeConstraint.SetMinimumSize)

        self.customLayout.setContentsMargins(48, 18, 44, 18)
        self.customLayout.addWidget(self.customLabel, 0, Qt.AlignmentFlag.AlignLeft)
        self.customLayout.addWidget(self.chooseButton, 0, Qt.AlignmentFlag.AlignRight)
        self.customLayout.setSizeConstraint(QHBoxLayout.SizeConstraint.SetMinimumSize)

        self.viewLayout.setSpacing(0)
        self.viewLayout.setContentsMargins(0, 0, 0, 0)
        self.addGroupWidget(self.radioWidget)
        self.addGroupWidget(self.customWidget)

    def __onRadioButtonClicked(self, button: RadioButton):
        """ radio button clicked slot """
        if button.text() == self.choiceLabel.text():
            return

        self.choiceLabel.setText(button.text())
        self.choiceLabel.adjustSize()

        if button is self.defaultRadioButton:
            self.chooseButton.setDisabled(True)
            cfg.set(self.configItem, self.default)
            if self.default != self.custom:
                self.fileChanged.emit(self.default)
        else:
            self.chooseButton.setDisabled(False)
            cfg.set(self.configItem, self.custom)
            if self.default != self.custom:
                self.fileChanged.emit(self.custom)

    def __showFileDialog(self):
        """ show file dialog slot """
        file, _ = QFileDialog.getOpenFileName(
            self,
            "选择背景图片",
            "",
            "Image Files (*.jpg *.png *.jpeg *.gif)"
        )
        if file:
            self.customLabel.setText(file)
            self.custom = file
            cfg.set(self.configItem, file)
            self.fileChanged.emit(file)